/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package game1;

import java.awt.event.MouseEvent;
import javax.swing.JPanel;

import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.LinkedList;
/**
 *
 * @author Юра
 */

public class Board extends JPanel implements Runnable,MouseListener,MouseMotionListener {
    private Thread animator;
    private final int DELAY=50;
    private Point mouseLocation=new Point(100, 100);
    private LinkedList<Friends> friends = new LinkedList();
    private LinkedList<Dots> dots = new LinkedList();


    
    public Board() {
        setBackground(Color.BLACK);
        setDoubleBuffered(true);
    }

    @Override
    public void addNotify() {
        super.addNotify();
        animator = new Thread(this);
        animator.start();
        

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        setBackground(Color.BLACK);
        setDoubleBuffered(true);
        Graphics2D g2 = (Graphics2D) g;
        RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2.setRenderingHints(rh);
        
        
        for (Friends f: friends)
        {
            f.draw(g2);
            for (Dots d: f.boollets)
            {
                d.draw(g2);
            }
        }

        for (Dots d: dots)
            d.draw(g2);
    }

    public void move() {

        for (Friends f: friends)
        {
            LinkedList<Dots> remove =new LinkedList();
            for (Dots d: f.boollets)
            {
                if (d.armed && d.get_v()==0)
                {
                    d.setColor(Color.YELLOW);
                    d.armed=Boolean.FALSE;
                    this.dots.add(d);
                    remove.add(d);
                }
            }
            f.boollets.removeAll(remove);

            f.settarget(mouseLocation);
            f.move();
        }

        for (Dots d: dots)
            d.move();

    }

    public void run() {
        long beforeTime, timeDiff, sleep;
        addMouseListener(this);
        addMouseMotionListener(this);

        friends.add(new Friends(100,100));
        friends.add(new Friends(100,150));
//        friends.add(new Friends(75,125,Color.GREEN));
//        friends.add(new Friends(125,125,Color.ORANGE));
//        friends.add(new Friends(100,125,Color.RED));
        
       
        beforeTime = System.currentTimeMillis();

        while (true) {

            move();
            repaint();

            timeDiff = System.currentTimeMillis() - beforeTime;
            sleep = DELAY - timeDiff;

            if (sleep < 0)
                sleep = 2;
            try {
                Thread.sleep(sleep);
            } catch (InterruptedException e) {
                System.out.println("interrupted"+sleep);
            }

            beforeTime = System.currentTimeMillis();
        }
    }

    public void mouseDragged(MouseEvent e) {   }

    public void mouseMoved(MouseEvent e) {
        mouseLocation = e.getPoint();
        }
    public void mouseClicked(MouseEvent e) {}

    public void mousePressed(MouseEvent e) {
       friends.add(new Minigun(e.getPoint().x, e.getPoint().y));

    }
    public void mouseReleased(MouseEvent e) {    }

    public void mouseEntered(MouseEvent e) {       }

    public void mouseExited(MouseEvent e) {  }


}